package com.qqtech.qquan.delivery.controller;

import java.sql.Timestamp;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.admin.context.AdminPage;
import com.qqtech.core.common.util.PinyingUtil;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.delivery.enums.DeliveryOrderDictTypeConstants;
import com.qqtech.qquan.delivery.model.DeliverySysDict;
import com.qqtech.qquan.delivery.model.DeliverySysDictQuery;
import com.qqtech.qquan.delivery.model.DeliverySysDictVo;
import com.qqtech.qquan.delivery.service.DeliverySysDictService;
import com.qqtech.qquan.sys.controller.BaseAdminController;

@Controller
@RequestMapping(value = "/delivery/sys/dict")
public class DeliverySysDictController extends BaseAdminController {
	@Resource
	private DeliverySysDictService deliverySysDictService;
    
	@Override
	protected BaseService<DeliverySysDict> getBaseService() {
		return deliverySysDictService;
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Model model) {
		return "/delivery/sys/dict-list";
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(DeliverySysDictQuery query,
			HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		Pageable pageKit = new PageKit(query.getPage(),query.getSize(), new PageOrder(
				Direction.ASC, "remark"));
		Page<DeliverySysDictVo> page = deliverySysDictService.queryPageList(
				query, pageKit);
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements()+""));
		return adminPage;
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(Model model) {
		return "/delivery/sys/dict-update";
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public Result doAdd(DeliverySysDictVo deliverySysDictVo, HttpServletRequest request,
			HttpServletResponse response) {
		Result ret = new Result();
		if (deliverySysDictVo == null) {
			ret.addErr("参数不能为空");
		} else {
			if (StringUtils.isBlank(deliverySysDictVo.getType())) {
				ret.addErr("请输入字典类型英文");
			}
			if (StringUtils.isBlank(deliverySysDictVo.getTypeDesc())) {
				ret.addErr("请输入字典类型中文");
			}
			if (StringUtils.isBlank(deliverySysDictVo.getName())) {
				ret.addErr("请输入字典name值");
			}
			if (StringUtils.isBlank(deliverySysDictVo.getCode())) {
				ret.addErr("请输入字典code值");
			}
		}
		if (ret.checkForm()) {
			Timestamp now = TimeUtil.now();
			DeliverySysDict entity = new DeliverySysDict();
			entity.setType(deliverySysDictVo.getType().trim());
			if (entity.getType().equals(DeliveryOrderDictTypeConstants.DICTYPE_DELIVERY_DELIVERY_NAME)) {
				entity.setTypeDesc("物流公司");
				entity.setRemark(PinyingUtil.getPinYin(deliverySysDictVo.getName().substring(0,1),true));
			} else {
				entity.setTypeDesc(deliverySysDictVo.getTypeDesc().trim());
				entity.setRemark(deliverySysDictVo.getRemark());
			}
			entity.setName(deliverySysDictVo.getName().trim());
			entity.setCode(deliverySysDictVo.getCode().trim());
			if (deliverySysDictVo.getOidx() == null) {
				entity.setOidx(0);
			} else {
				entity.setOidx(deliverySysDictVo.getOidx());
			}
			entity.setNewAt(now);
			entity.setUpdAt(now);
			deliverySysDictService.insert(entity);
		}
		return ret;
	}
	
	@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
	public String update(@PathVariable Integer id, HttpServletRequest request, HttpServletResponse response) {
		DeliverySysDictVo entity = deliverySysDictService.getById(id);
		request.setAttribute("entity", entity);
		return "/delivery/sys/dict-update";
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public Result doUpdate(DeliverySysDictVo deliverySysDictVo) {
		Result ret = new Result();
		if (deliverySysDictVo == null || deliverySysDictVo.getId() == null) {
			ret.addErr("参数不能为空");
		} else {
			if (StringUtils.isBlank(deliverySysDictVo.getType())) {
				ret.addErr("请输入字典类型英文");
			}
			if (StringUtils.isBlank(deliverySysDictVo.getTypeDesc())) {
				ret.addErr("请输入字典类型中文");
			}
			if (StringUtils.isBlank(deliverySysDictVo.getName())) {
				ret.addErr("请输入字典name值");
			}
			if (StringUtils.isBlank(deliverySysDictVo.getCode())) {
				ret.addErr("请输入字典code值");
			}
		}
		if (ret.checkForm()) {
			Timestamp now = TimeUtil.now();
			DeliverySysDict entity = new DeliverySysDict();
			entity.setId(deliverySysDictVo.getId());
			entity.setType(deliverySysDictVo.getType().trim());
			if (entity.getType().equals(DeliveryOrderDictTypeConstants.DICTYPE_DELIVERY_DELIVERY_NAME)) {
				entity.setTypeDesc("物流公司");
				entity.setRemark(PinyingUtil.getPinYin(deliverySysDictVo.getName().substring(0,1),true));
			} else {
				entity.setTypeDesc(deliverySysDictVo.getTypeDesc().trim());
				entity.setRemark(deliverySysDictVo.getRemark());
			}
			entity.setName(deliverySysDictVo.getName().trim());
			entity.setCode(deliverySysDictVo.getCode().trim());
			if (deliverySysDictVo.getOidx() == null) {
				entity.setOidx(0);
			} else {
				entity.setOidx(deliverySysDictVo.getOidx());
			}
			entity.setUpdAt(now);
			deliverySysDictService.updateById(entity);
		}
		return ret;
	}
	
	@RequestMapping(value = "/doDele", method = RequestMethod.POST)
	@ResponseBody
	public Result doDele(DeliverySysDictVo deliverySysDictVo) {
		Result ret = new Result();
		if (deliverySysDictVo == null || deliverySysDictVo.getId() == null) {
			ret.addErr("参数不能为空");
		}
		if (ret.checkForm()) {
			int v = deliverySysDictService.deleteById(deliverySysDictVo.getId());
			if (v == 0) {
				ret.addErr("删除不成功");
			}
		}
		return ret;
	}
}
